package com.sy.simpleegg.platform.ychsdk.manager

import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.platform.ychsdk.bean.HeartResp
import com.sy.simpleegg.platform.ychsdk.bean.YchBuyPackageBean
import com.sy.simpleegg.platform.ychsdk.bean.YchBuyPackageResp
import com.sy.simpleegg.platform.ychsdk.bean.config.GetPayCoinData
import com.sy.simpleegg.platform.ychsdk.bean.config.GetPayQrCodeData
import com.sy.simpleegg.platform.ychsdk.bean.good.GoodsCoinResponse
import com.sy.simpleegg.platform.ychsdk.bean.good.RechargeGoodsData
import com.sy.simpleegg.platform.ychsdk.bean.member.GetMemberLoginQrCode
import com.sy.simpleegg.platform.ychsdk.bean.member.QrCodeMemberInfoResponse
import com.sy.simpleegg.platform.ychsdk.bean.member.SaveCoinResult
import com.sy.simpleegg.platform.ychsdk.bean.pay.ChannelWaitOffData
import com.sy.simpleegg.platform.ychsdk.bean.pay.PayResult
import com.sy.simpleegg.platform.ychsdk.bean.pay.RepCashDataBean
import com.sy.simpleegg.platform.ychsdk.loader.BusinessLoader
import com.sy.simpleegg.util.GsonUtil
import com.ych.ytdevicesdk.api.BaseResult
import io.reactivex.Observer

class YchBusinessManager {

//    companion object {
//        var instance: IBusinessManager? = null
//    }

    private val businessLoader: BusinessLoader = BusinessLoader()

    private var isDebug = false

    private var useTestUrl = false

    companion object {
        private var instance: YchBusinessManager? = null
            get() {
                if (field == null) {
                    field = YchBusinessManager()
                }
                return field
            }
        @Synchronized
        fun get(): YchBusinessManager{
            return instance!!
        }
    }

    fun getPackageList(observer: Observer<BaseResult<GoodsCoinResponse?>>?){
        val map: Map<String?, Boolean?> = mapOf("OnlyQueryMeiTuanGoods" to false)
        businessLoader.getPackageList(map, observer)
    }

    fun getRechargeList(devNum: String, observer: Observer<BaseResult<RechargeGoodsData?>>?){
        val map: Map<String?, String?> = mapOf("DevNum" to devNum)
        businessLoader.getRechargeList(map, observer)
    }

    fun getConfig(observer: Observer<BaseResult<GetPayCoinData?>>?){
        businessLoader.getConfig(mapOf(), observer)
    }

    fun repCashData(repCashData: RepCashDataBean, observer: Observer<BaseResult<Any?>>?){
        val map = mapOf<String?, Any?>(
            Pair("TransID", repCashData.TransID),
            Pair("Count", repCashData.Count),
            Pair("Money", repCashData.Money),
            Pair("MachineBalance", repCashData.MachineBalance),
            Pair("OutStatus", repCashData.OutStatus),
            Pair("Expected", repCashData.Expected),
            Pair("SerialPortInfo", repCashData.SerialPortInfo),
            Pair("CreateTime", repCashData.CreateTime),
            Pair("Vending", repCashData.Vending),
            Pair("Msg", repCashData.Msg),
        )

        businessLoader.pullCashCoinResult(map, observer)
    }

    /**
     * 下单
     */
    fun placeOrder(buyPackageBean: YchBuyPackageBean, observer: Observer<BaseResult<YchBuyPackageResp?>>?){
        val map = if (buyPackageBean.PayCode.isEmpty()){
            mapOf<String?, Any?>(
                Pair("TotalMoney", buyPackageBean.TotalMoney),
                Pair("TotalCoin", buyPackageBean.TotalCoin),
                Pair("PayType", buyPackageBean.PayType),
                Pair("TransID", buyPackageBean.TransID),
                Pair("DevNum", buyPackageBean.DevNum),
                Pair("Items", buyPackageBean.Items),
            )
        }else{
            mapOf<String?, Any?>(
                Pair("TotalMoney", buyPackageBean.TotalMoney),
                Pair("TotalCoin", buyPackageBean.TotalCoin),
                Pair("PayType", buyPackageBean.PayType),
                Pair("PayCode", buyPackageBean.PayCode),
                Pair("TransID", buyPackageBean.TransID),
                Pair("DevNum", buyPackageBean.DevNum),
                Pair("Items", buyPackageBean.Items),
            )
        }
        logFile("下单数据：${GsonUtil.getInstance()?.toJson(map)}")
        businessLoader.placeOrder(map, observer)
    }

    /**
     * 获取消费二维码
     */
    fun getConsumptionCode(observer: Observer<BaseResult<GetPayQrCodeData?>>?){
        val map = mapOf<String?, Any?>()
        businessLoader.getConsumptionCode(map, observer)
    }

    /**
     * 心跳
     */
    fun heart(netDelay: String, wifiStrength: String, apMac: String, mqOnline: String, observer: Observer<BaseResult<HeartResp?>>?){
        val map = mapOf<String?, Any?>(
            Pair("NetDelay", netDelay),
            Pair("WifiStrength", wifiStrength),
            Pair("APMac", apMac),
            Pair("MqOnline", mqOnline),
        )
        businessLoader.heart(map, observer)
    }

    /**
     * 查询订单支付结果
     */
    fun getPayResult(orderId: String, observer: Observer<BaseResult<PayResult?>>?){
        val map = mapOf<String?, Any?>(
            Pair("OrderID", orderId),
        )
        businessLoader.queryPayOrder(map, observer)
    }

    /**
     * 获取登录二维码
     */
    fun getMemberLoginCode(devNumber: String, observer: Observer<BaseResult<GetMemberLoginQrCode?>>?){
        val map = mapOf<String?, Any?>(
            Pair("DevNum", devNumber),
        )
        businessLoader.getMemberLoginCode(map, observer)
    }

    /**
     * 查询会员扫码登录结果
     *
     */
    fun queryLgLogin(transId: String, observer: Observer<BaseResult<QrCodeMemberInfoResponse?>>?){
        val map = mapOf<String?, Any?>(
            Pair("TransID", transId),
        )
        businessLoader.queryLgLogin(map, observer)
    }

    /**
     * 会员存币
     * @param leaguerId 会员ID
     * @param num 存币数
     * @param devNum 设备编号
     * @param remark 备注
     * @param transId 事务ID
     */
    fun repSaveCoinsData(leaguerId: String, num: Long, devNum: String, remark: String, transId: String, observer: Observer<BaseResult<SaveCoinResult?>>?){
        val map = mapOf<String?, Any?>(
            Pair("LeaguerID", leaguerId),
            Pair("Num", num),
            Pair("DevNum", devNum),
            Pair("Remark", remark),
            Pair("TransID", transId),
        )
        businessLoader.repSaveCoinsData(map, observer)
    }

    /**
     * 上报核销数据
     * @param leaguerId 会员ID
     * @param num 存币数
     * @param devNum 设备编号
     * @param remark 备注
     * @param transId 事务ID
     */
    fun repWriteOffData(code: String, transId: String, amount: Long, channel: String, devNum: String, isSendMq: Boolean, source: Int, observer: Observer<BaseResult<ChannelWaitOffData?>>?){
        val map = mapOf<String?, Any?>(
            Pair("Code", code),
            Pair("Amount", amount),
            Pair("Channel", channel),
            Pair("DevNum", devNum),
        )
        businessLoader.repWriteOffData(map, observer)
    }

    /**
     * 上报非MQ出币结果
     * @param realOutCoins 实出币数
     * @param needOutCoins 应出币数
     * @param result 出币状态： 0-成功，1-马达卡币  2-机台币不足
     * @param balance 机台余币
     * @param orderId 订单id
     * @param transId 事务id
     */
    fun repMqOutCoinResult(realOutCoins: Long, needOutCoins: Long, result: Int, balance: Long, orderId: String, transId: String, observer: Observer<BaseResult<Any?>>?){
        val map = mapOf<String?, Any?>(
            Pair("Count", realOutCoins),
            Pair("Expected", needOutCoins),
            Pair("Status", result),
            Pair("Balance", balance),
            Pair("OrderID", orderId),
            Pair("TransID", transId),
        )
        businessLoader.repMqOutCoinResult(map, observer)
    }

    /**
     * 上报非MQ出币结果
     * @param result 0 成功 其他失败
     * @param orderId 订单id
     * @param msg 错误描述
     */
    fun repNotMqOutCoinResult(result: Int, orderId: String, msg: String, observer: Observer<BaseResult<Any?>>?){
        val map = mapOf<String?, Any?>(
            Pair("Result", result),
            Pair("Message", orderId),
            Pair("OrderID", msg)
        )
        businessLoader.repOutCoinResult(map, observer)
    }

}